// pages/cart/cart.js
import {request} from "../../request/index.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    totalPrice:0,
    totalNum:0
  },
  handleOrderPay(){
    try {
      if (!wx.getStorageSync("userInfo")){
        wx.navigateTo({
          url:'/pages/login/login'
        })
        return;
      }
      const token=wx.getStorageSync("token")
      if (!token){
        wx.navigateTo({
          url:'/pages/auth/auth'
        })
        return
      }
      // const header={Authorization	:token};
      const order_price=this.data.totalPrice
      const addressAll=this.data.address.provinceName+
          this.data.address.cityName+this.data.address.countyName+this.data.address.detailInfo
      const consignee_addr=addressAll
      const cart=this.data.cart
      let goods=[]
      cart.forEach(v=>goods.push({
        goods_id:v.goods_id,
        goods_number:v.num,
        goods_price: v.goods_price
      }))
      const orderParams={order_price,consignee_addr,goods};
      request({url:"/my/orders/create",method:"POST",data:orderParams}).then(res=>{
        let order_number=res.data.message.order_number
        request({url:"/my/orders/req_unifiedorder",method:"POST",data:{order_number}}).then(res=>{
          let pay=res.data.message.pay
          wx.requestPayment({
            ...pay
          })
          request({url:"/my/orders/chkOrder",method:"POST",data:{order_number}}).then(res=>{
            wx.showToast({
              title:"支付成功！"
            })
            let newCart=wx.getStorageSync("cart")
            newCart=newCart.filter(v=>!v.checked)
            wx.setStorageSync("cart",newCart)
          })
        })
      })
    }catch (err){
      wx.showToast({
        title:"支付失败！"
      })
      console.log(err);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address=wx.getStorageSync("address");
    let cart=wx.getStorageSync("cart")||[];
    cart=cart.filter(v=>v.checked);
    let totalPrice=0;
    let totalNum=0;
    cart.forEach(v=>{
      totalPrice+=v.num*v.goods_price;
      totalNum +=v.num
    })
    this.setData({
      address,
      cart,
      totalNum,
      totalPrice
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})